from werkzeug.datastructures import MultiDict
from flask_restful import reqparse


# 重写reqpaesr中的Argument方法，实现参数大小写不敏感

class CaseInsensitiveMultiDict(MultiDict):
    def __init__(self, mapping=None):
        super().__init__(mapping)
        # map lowercase keys to the real keys
        self.lower_key_map = {key.lower(): key for key in self}

    def __contains__(self, key):
        return key.lower() in self.lower_key_map

    def getlist(self, key):
        return super().getlist(self.lower_key_map.get(key.lower()))

    def pop(self, key):
        return super().pop(self.lower_key_map.get(key.lower()))


class CaseInsensitiveArgument(reqparse.Argument):
    def source(self, request):
        return CaseInsensitiveMultiDict(super().source(request))
